Conditions | 7 |
Total Lines | 41 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import type {CollectingArg, InternalOptions} from "./$defs.types" |
||
5 | |||
6 | function collector( |
||
7 | identifiers: Record<string, true>, |
||
8 | { |
||
9 | identifierParser, |
||
10 | identifierMatchIndex, |
||
11 | identifierCleanupParser, |
||
12 | identifierCleanupReplace, |
||
13 | allowedAtRuleNames |
||
14 | }: Pick<Required<Options>, "identifierMatchIndex"|"identifierCleanupReplace"> |
||
15 | & Pick<InternalOptions, "identifierParser"|"identifierCleanupParser"|"allowedAtRuleNames"> |
||
16 | ) { |
||
17 | return ({selectors, parent}: CollectingArg) => { |
||
18 | if (parent?.type === "atrule") { |
||
19 | const {name} = parent |
||
20 | |||
21 | if (name && !allowedAtRuleNames.has(name)) |
||
22 | return |
||
23 | } |
||
24 | |||
25 | //TODO consider postcss-selector-parser |
||
26 | const {length} = selectors |
||
27 | |||
28 | for (let i = length; i--;) { |
||
29 | const selector = selectors[i] |
||
30 | |||
31 | let parsed: RegExpExecArray | null, |
||
32 | lastIndex: number|undefined = undefined |
||
33 | |||
34 | while (parsed = identifierParser.exec(selector)) { |
||
35 | const {index} = parsed |
||
36 | |||
37 | if (index === lastIndex) |
||
38 | // TODO consider throw error |
||
39 | return |
||
40 | |||
41 | lastIndex = index |
||
42 | const identifier = parsed[identifierMatchIndex] |
||
43 | .replace(identifierCleanupParser, identifierCleanupReplace) |
||
44 | |||
45 | identifiers[identifier] = true |
||
46 | } |
||
51 |